*********************************************************************
8/22/07: I converted this to a macro and added the tax variables.

THESE NOTES WERE IN THE PROGRAM, BUT FOR SOME REASONS, THIS SCREWS UP
THE MACRO, SO I'VE MOVED THEM TO THE TOP
* apparently some related subfamilies don't have a primarty family so do:
	if fkind=2 and ftotval_final=. then ftotval_final=ftotval...

* one family has fpersons=90 when it's actually 6, famid 1100052 so do:
	if fpersons_final=90 then fpersons_final=6

DEFINE SM'S AS:
	never married, divorced, separated, or widowed
	at least one own never-married kid under 18
	primary families or unrelated subfamilies 

* When calc. houshold level income etc. exclude the related subfamilies 
	because their values of the following are 
    already included in the values for the primary family;

* HSEQ=13 HAS WEIRD FPERSONS=90 AND VERY LARGE FTOTVAL

6/4/10: added age of head for melly decomps.

8/9/2011: FIX BUG WHERE FAMILIES WEREN'T SORTED BY PERSON WITHIN A FAMILY 
		  WHEN USING FIRST.*.  IT ONLY AFFECTED A SMALL FRACTION OF OBS.  
		  FIXED BUG BY SORTING BY PPOS IN ADDITION TO FAMID OR HSEQ.  
		  VARIABLES AFFECTED: DEM_GRP_PRIME, EMP_HEAD_PRIME, ED_HEAD_PRIME, AGE_HD_PRIME

**********************************************************************;


options obs=max mprint;

%macro clean7677(yr);

data poverty;
set cpsdata.all&yr;

proc sort data=poverty;
BY famid;
RUN;

data poverty;
   set poverty;
   by famid;
*** added by JH ***;
RETAIN counter 0; 
counter= counter+1;
IF FIRST.famid THEN DO; 
   counter=1;
END;

*** edited by JH ***;
IF 1972 LE &yr LE 1975 THEN id=famid*100+counter;
   ELSE if &yr GE 1976 THEN id=pseq*100+ppos;
ftot_trans=fssinc+fssi+fuiinc+fpawval; 
run;
PROC SORT DATA=poverty;
   BY id;
RUN;
PROC SORT DATA=taxsim.new_taxs&yr;
   BY id;
RUN;
DATA poverty;
MERGE poverty(IN=in1) taxsim.new_taxs&yr(DROP=year);
BY id;
IF in1;

IF 1972 LE &yr LE 1977 THEN ts_tottax=ftl+taxs+(ts_fica/2);
   ELSE if &yr GE 1978 THEN ts_tottax=ftl+stl+(ts_fica/2);

emp=(wkswrk>0);
kid=(age LE 17);
run;

PROC SORT DATA=poverty;
   BY famid;
RUN;
proc means sum noprint data=poverty;
   by famid;
   var ts_tottax emp kid;
   output out=tmp1(drop= _type_ _freq_) sum=ftax_nonprime no_earnr_nonprime no_kids_nonprime;
run;

DATA poverty;
MERGE poverty(IN=in1) tmp1(KEEP=famid ftax_nonprime no_earnr_nonprime no_kids_nonprime);
BY famid;
IF in1;
run;

PROC SORT DATA=poverty;
   BY hseq;
RUN;
proc means sum noprint data=poverty;
   by hseq;
   where fkind IN(1,2);
   var ts_tottax emp kid;
   output out=tmp2(drop= _type_ _freq_) sum=ftax_prime no_earnr_prime no_kids_prime;
run;
proc means sum noprint data=poverty;
   by hseq;
   var ts_tottax;
   output out=tmp3(drop= _type_ _freq_) sum=hts_tottax;
run;

DATA poverty;
MERGE poverty(IN=in1) tmp2(KEEP=hseq ftax_prime no_earnr_prime no_kids_prime) 
					  tmp3(KEEP=hseq hts_tottax);
BY hseq;
IF in1;

IF fkind IN(1,2) THEN fts_tottax=ftax_prime;
   ELSE fts_tottax=ftax_nonprime;
IF fkind IN(1,2) THEN tot_earnrs=no_earnr_prime;
   ELSE tot_earnrs=no_earnr_nonprime;
IF fkind IN(1,2) THEN tot_kids=no_kids_prime;
   ELSE tot_kids=no_kids_nonprime;
run;

data temp;
set poverty;
if fkind=1;
run;

data temp;     
set temp;    
by hseq notsorted;
if first.hseq=1 then dummy=1;
else dummy=0;
if dummy=1;
ftotval_new=ftotval;
ftotearn_new= ftotearn;
fpovcut_new=fpovcut;
fpersons_new=fpersons;
frelu18_new=frelu18;
ftot_trans_new=ftot_trans;
fssinc_new=fssinc;

keep hseq ftotval_new fpovcut_new ftotearn_new fpersons_new frelu18_new ftot_trans_new fssinc_new;
run;

proc sort data=temp;
by hseq;
run;
proc sort data=poverty;
by hseq;
run;

data poverty;
merge poverty temp;
by hseq;

if fkind=2 then ftotval_final=ftotval_new;
else ftotval_final=ftotval;
if fkind=2 then fpovcut_final=fpovcut_new;
else fpovcut_final=fpovcut;
if fkind=2 then ftotearn_final= ftotearn_new;
else ftotearn_final=ftotearn;
if fkind=2 then fpersons_final= fpersons_new;
else fpersons_final=fpersons;
if fkind=2 then frelu18_final= frelu18_new;
else frelu18_final=frelu18;

if fkind=2 then ftot_trans_final=ftot_trans_new;
else ftot_trans_final=ftot_trans;
if fkind=2 then fssinc_rr_final=fssinc_new;
else fssinc_rr_final=fssinc;



if fkind=2 and ftotval_final=. then ftotval_final=ftotval;
if fkind=2 and fpovcut_final=. then fpovcut_final=fpovcut;
if fkind=2 and ftotearn_final=. then ftotearn_final=ftotearn;
if fkind=2 and fpersons_final=. then fpersons_final=fpersons;
if fkind=2 and frelu18_final=. then frelu18_final=frelu18;
if fkind=2 and ftot_trans_final=. then ftot_trans_final=ftot_trans;
if fkind=2 and fssinc_rr_final=. then fssinc_rr_final=fssinc;

if fpersons_final=90 then do;
  fpersons_final=6;
  hts_tottax=0;
  fts_tottax=0;
  fpovcut_final=7406;
  fpovcut=7406;
  ftotearn_final=592;
  ftotearn=592;
end;

run;

proc sort data=poverty;
   by famid ppos;
run;

data temp;
   set poverty;
   by famid ppos;
   if first.famid; 

      IF marstat IN (5,6,7,8) AND fownu18 GT 0 AND fkind IN (1,3) 
      and sex=2 and 18 LE age LE 54 then sm_f=1;
   ELSE sm_f=0;

  emp_hd_nprime=(wkswrk>0);

  age_hd_nprime=age;

  IF age LE 64 AND tot_kids GT 0 AND marstat IN (5,6,7,8) THEN dem_grp_nprime=1;
   ELSE IF age LE 64 AND tot_kids GT 0 AND marstat IN (1,2,3,4) THEN dem_grp_nprime=2;
   ELSE IF age LE 64 AND tot_kids=0 AND marstat IN (5,6,7,8) THEN dem_grp_nprime=3;
   ELSE IF age LE 64 AND tot_kids=0 AND marstat IN (1,2,3,4) THEN dem_grp_nprime=4;
   ELSE IF age GE 65 THEN dem_grp_nprime=5;

   IF hga LE 12 OR (hga=13 AND hgc NE 1) THEN ed_hd_nprime=1;
      ELSe IF hga=13 AND hgc=1 THEN ed_hd_nprime=2;
      ELSE IF 14 LE hga LE 16 OR (hga=17 AND hgc NE 1) THEN ed_hd_nprime=3;
      ELSE IF hga GT 17 OR (hga=17 AND hgc=1) THEN ed_hd_nprime=4;

run;
proc sort data=temp;
   by famid;
run;

DATA poverty;
MERGE poverty(IN=in1) temp(keep=famid sm_f emp_hd_nprime age_hd_nprime dem_grp_nprime ed_hd_nprime);
by famid;
run;

proc sort data=poverty;
   by hseq ppos;
run;
proc sort data=temp;
   by hseq;
run;

proc means max noprint data=poverty;
by hseq;
var refrel2 ;
   output out=temp3 max=fammax ;
run;

data temp2;
set temp(rename=(sm_f=sm_h));
   IF fkind=1;
run;


proc means sum noprint data=temp;
   by hseq;
   where fkind NE 2;
   var ftotearn_final fpersons_final frelu18_final ftot_trans_final fssinc_rr_final;
   output out=temp1(drop= _type_ _freq_) sum=htotearn hpersons hrelu18 htot_trans hssinc_rr;
run;

data temp4;
   set poverty;
   by hseq ppos;
   if first.hseq; 

emp_hd_prime=(wkswrk>0);
age_hd_prime=age;

  IF age LE 64 AND tot_kids GT 0 AND marstat IN (5,6,7,8) THEN dem_grp_prime=1;
   ELSE IF age LE 64 AND tot_kids GT 0 AND marstat IN (1,2,3,4) THEN dem_grp_prime=2;
   ELSE IF age LE 64 AND tot_kids=0 AND marstat IN (5,6,7,8) THEN dem_grp_prime=3;
   ELSE IF age LE 64 AND tot_kids=0 AND marstat IN (1,2,3,4) THEN dem_grp_prime=4;
   ELSE IF age GE 65 THEN dem_grp_prime=5;

   IF hga LE 12 OR (hga=13 AND hgc NE 1) THEN ed_hd_prime=1;
      ELSe IF hga=13 AND hgc=1 THEN ed_hd_prime=2;
      ELSE IF 14 LE hga LE 16 OR (hga=17 AND hgc NE 1) THEN ed_hd_prime=3;
      ELSE IF hga GT 17 OR (hga=17 AND hgc=1) THEN ed_hd_prime=4;

KEEP hseq emp_hd_prime age_hd_prime dem_grp_prime ed_hd_prime;
run;

data cpsdata.poverty&yr;
   merge poverty temp1(keep=hseq htotearn hpersons hrelu18 htot_trans hssinc_rr) 
				 temp2(keep=hseq sm_h) 
				 temp3(keep=hseq fammax)
				 temp4(keep=hseq emp_hd_prime age_hd_prime dem_grp_prime ed_hd_prime);
   by hseq;

if fkind=2 and htotearn=. then htotearn=ftotearn_final;
if fkind=2 and hpersons=. then hpersons=fpersons_final;
if fkind=2 and hrelu18=. then hrelu18=frelu18_final;
if fkind=2 and htot_trans=. then htot_trans=ftot_trans_final;
if fkind=2 and hssinc_rr=. then hssinc_rr=fssinc_rr_final;

   IF fkind ne 2 then sm=sm_f;
   ELSE IF fkind=2 then sm=sm_h;

   IF fkind=1 and sm=1 and fammax LE 4 then smlone=1;
   ELSE smlone=0;

IF fkind IN(1,2) THEN DO;
	dem_grp=dem_grp_prime;
	ed_head=ed_hd_prime;
	emp_head=emp_hd_prime;
	age_head=age_hd_prime;
END;
IF fkind NOT IN(1,2) THEN DO;
	dem_grp=dem_grp_nprime;
	ed_head=ed_hd_nprime;
	emp_head=emp_hd_nprime;
	age_head=age_hd_nprime;
END;
RUN;
proc means;
run;

%mend;

%clean7677(1976);
%clean7677(1977);



